function r51_idscreator(ncid, natm) result(ids)

    use iasing_osse
    use netcdf
    
    implicit none
    ! Arguments
    integer, intent(in)         :: ncid
    integer(JPI4), intent(in)   :: natm
    ! Output
    type(r4a_ids)               :: ids
    ! Internal variables
    integer :: io
    
    
    
    ! Define dimensions
    io = nf90_def_dim(ncid, "atmosphere", natm, ids%atm_did)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'atmosphere' dimension id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_dim(ncid, "level", NLM51, ids%lev_did)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'level' dimension id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_dim(ncid, "wnu_iasi", NIASI, ids%wia_did)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'wnu_iasi' dimension id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_dim(ncid, "wnu_iang", NIANG, ids%wng_did)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'wnu_iang' dimension id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_dim(ncid, "ObsChannelList", NCHAN, ids%ocl_did)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'ObsChannelList' dimension id..."
        print*, nf90_strerror(io)
        print*
    endif
    
    
    
    ! Dimensions group
    io = nf90_def_grp(ncid, "Dimensions", ids%dim%gid)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'Dimensions' group id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%dim%gid, "atmosphere", NF90_INT, &
                      (/ids%atm_did/), ids%dim%atm)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'atmosphere' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%dim%gid, "level", NF90_FLOAT, &
                      (/ids%lev_did/), ids%dim%lev)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'level' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%dim%gid, "wnu_iasi", NF90_FLOAT, &
                      (/ids%wia_did/), ids%dim%wia)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'wnu_iasi' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%dim%gid, "wnu_iang", NF90_FLOAT, &
                      (/ids%wng_did/), ids%dim%wng)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'wnu_iang' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%dim%gid, "ObsChannelList", NF90_INT, &
                      (/ids%ocl_did/), ids%dim%ocl)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'ObsChannelList' variable id..."
        print*, nf90_strerror(io)
        print*
    endif

    
    
    ! Coordinates group
    io = nf90_def_grp(ncid, "Coordinates", ids%coo%gid)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'Coordinates' group id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%coo%gid, "lon", NF90_FLOAT, &
                      (/ids%atm_did/), ids%coo%lon)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'lon' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%coo%gid, "lat", NF90_FLOAT, &
                      (/ids%atm_did/), ids%coo%lat)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'lat' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%coo%gid, "ele", NF90_FLOAT, &
                      (/ids%atm_did/), ids%coo%ele)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'ele' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%coo%gid, "dat", NF90_INT, &
                      (/ids%atm_did/), ids%coo%dat)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'dat' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%coo%gid, "tim", NF90_INT, &
                      (/ids%atm_did/), ids%coo%tim)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'tim' variable id..."
        print*, nf90_strerror(io)
        print*
    endif

    
    
    ! Observation parameters group
    io = nf90_def_grp(ncid, "Observation parameters", ids%opa%gid)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'Observation parameters' group id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%opa%gid, "psu", NF90_FLOAT, &
                      (/ids%atm_did/), ids%opa%psu)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'psu' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%opa%gid, "tsu", NF90_FLOAT, &
                      (/ids%atm_did/), ids%opa%tsu)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'tsu' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%opa%gid, "iza", NF90_FLOAT, &
                      (/ids%atm_did/), ids%opa%iza)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'iza' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%opa%gid, "iaa", NF90_FLOAT, &
                      (/ids%atm_did/), ids%opa%iaa)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'iaa' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%opa%gid, "sza", NF90_FLOAT, &
                      (/ids%atm_did/), ids%opa%sza)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'sza' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%opa%gid, "saa", NF90_FLOAT, &
                      (/ids%atm_did/), ids%opa%saa)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'saa' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%opa%gid, "lsm", NF90_FLOAT, &
                      (/ids%atm_did/), ids%opa%lsm)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'lsm' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%opa%gid, "clc", NF90_INT, &
                      (/ids%atm_did/), ids%opa%clc)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'clc' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%opa%gid, "emi", NF90_INT, & 
                      (/ids%atm_did/), ids%opa%emi)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'emi' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%opa%gid, "ert", NF90_SHORT, &
                      (/ids%wng_did, ids%atm_did/), ids%opa%ert)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'ert' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%opa%gid, "rst", NF90_INT, & 
                      (/ids%atm_did/), ids%opa%rst)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'rst' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    
    
    
    ! Vertical profiles group
    io = nf90_def_grp(ncid, "Vertical profiles", ids%vpr%gid)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'Vertical profiles' group id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%vpr%gid, "tem", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%vpr%tem)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'tem' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%vpr%gid, "q", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%vpr%q)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'q' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%vpr%gid, "co2", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%vpr%co2)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'co2' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%vpr%gid, "o3", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%vpr%o3)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'o3' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%vpr%gid, "co", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%vpr%co)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'co' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%vpr%gid, "ch4", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%vpr%ch4)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'ch4' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%vpr%gid, "so2", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%vpr%so2)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'so2' variable id..."
        print*, nf90_strerror(io)
        print*
    endif



    ! Cloud information group
    io = nf90_def_grp(ncid, "Cloud information", ids%cld%gid)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'Cloud information' group id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%cld%gid, "cc", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%cld%cc)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'cc' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%cld%gid, "iwc", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%cld%iwc)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'iwc' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%cld%gid, "lwc", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%cld%lwc)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'lwc' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%cld%gid, "rwc", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%cld%rwc)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'rwc' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%cld%gid, "swc", NF90_FLOAT, &
                      (/ids%lev_did, ids%atm_did/), ids%cld%swc)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'swc' variable id..."
        print*, nf90_strerror(io)
        print*
    endif



    ! Radiances group
    io = nf90_def_grp(ncid, "Radiances", ids%rbt%gid)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'Radiances' group id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%rbt%gid, "ria", NF90_FLOAT, &
                      (/ids%wia_did, ids%atm_did/), ids%rbt%ria)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'ria' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%rbt%gid, "rng", NF90_FLOAT, &
                      (/ids%wng_did, ids%atm_did/), ids%rbt%rng)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'rng' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%rbt%gid, "ngb", NF90_FLOAT, &
                      (/ids%wng_did, ids%atm_did/), ids%rbt%ngb)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'ngb' variable id..."
        print*, nf90_strerror(io)
        print*
    endif

    
    
    ! Observations group
    io = nf90_def_grp(ncid, "Observations", ids%obs%gid)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'Observations' group id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%obs%gid, "oma", NF90_INT, &
                      (/ids%atm_did/), ids%obs%oma)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'oma' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%obs%gid, "obt", NF90_FLOAT, &
                      (/ids%ocl_did, ids%atm_did/), ids%obs%obt)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'obt' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%obs%gid, "obi", NF90_FLOAT, &
                      (/ids%ocl_did, ids%atm_did/), ids%obs%obi)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'obi' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    io = nf90_def_var(ids%obs%gid, "ofd", NF90_FLOAT, &
                      (/ids%ocl_did, ids%atm_did/), ids%obs%ofd)
    if (io /= NF90_NOERR) then
        print*, "Error defining 'ofd' variable id..."
        print*, nf90_strerror(io)
        print*
    endif
    
end function r51_idscreator
